import React from 'react'
// ===================================================================== Temp
import TableModal from '#cpt/temp/TableModal'
import TableControls from '#cpt/temp/TableControls'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== Lazy
const RecallModal = $Lazy.state(()=>import('./recall-modal'))
const message 		= $Lazy.file(()=>import('@antd/message'))
const Checkbox  = $Lazy.load(()=>import('@antd/form/checkbox'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		selectedKeys:[]
	}
	
	model = {}
	
	use_old_approve_at = false // 是否使用前次报告时间
	
	open = (spec_code,uuid) => {
		this.modal.open({ formRef:this.formRef })
		this.query = { spec_code,uuid }
		this.fetch()
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'sp-info-modify/reportall', { paging:'get' })
	
	// 表单
	cols = [
		{ title: '报告单编号', 	dataIndex: 'spec_num', 				...$fn.table.spec_num },
		{ title: '条码', 		dataIndex: 'spec_code', 			...$fn.table.spec_code },
		{ title: '科室', 		dataIndex: 'project_name', 			width:200 },
		{ title: '岗位', 		dataIndex: 'parent_project_name', 	width:200 },
		TableControls.controls.call(this, { width: 200}),
	]
	
	// 表格操作按钮
	table_controls = (text,{ tid, spec_num, spec_code, status }) => {
		const {uuid} = this.query
		return [		// 编辑
			+status === 95 && TableControls.click.call(this, '召回', ()=> this.RecallModal.open(false, [tid], uuid)),
			+status === 99 && TableControls.click.call(this, '批准', ()=>{
				$http.submit(this, 'sp-info-modify/approvereport', { param:{ id:tid, page_source: 'kf', use_old_approve_at: this.use_old_approve_at ? '1' : '0' } }).then(()=>{
					message.then(f=>f.success('操作成功'))
					this.fetch()
				})
			} ),
			TableControls.pushTo.call(this,'预览', `preview-report?spec_num=${spec_num}&spec_code=${spec_code}`),
		]
	}
	
	render(){
		return (
			<>
				{
					TableModal.call(this,{ title: '预览报告单', okText:'批量召回', labelWidth:'s7', width:'60%', height:'60%', idStr:'tid',
						table: {
							suffix: (
								<Checkbox label='使用前次报告时间' value={this.use_old_approve_at} onChange={ () => this.use_old_approve_at = !this.use_old_approve_at } />
							)
						},
						onOk: keys => {
							this.RecallModal.open(true, keys, this.query.uuid)
						}
					})
				}
				{/* 召回报告单 */}
				<RecallModal onRef={ ref => this.RecallModal = ref} onRefresh={()=>{
					this.fetch().then(data=> $fn.clearKeys.call(this) )
				}} />
			</>
		)
	}
}

export default Index